﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace States
{
    public class Gameplay : BaseState
    {
        private static Gameplay theGameplay;
        public Systems.TextureManager theTextureManager;
        Objects.Player thePlayer;
        public Systems.ObjectManager theObjectManager;
        float TankSpawnCounter = 0;
        private Gameplay()
        {
          
        }

        public static Gameplay Instance
        {
            get
            {
                if (theGameplay == null)
                    theGameplay = new Gameplay();
                return theGameplay;
            }
        }

        public void AddToScore()
        {
            thePlayer.PlayerScore += 10;
        }
        public override void Enter()
        {
            theDevice = TopDownShooter.Game1.deviceOfGraphicsCards;
            theContentManager = TopDownShooter.Game1.loadingManager;
            theSpriteBatch = TopDownShooter.Game1.spriteBatch;

            theTextureManager = new Systems.TextureManager(theContentManager);
            LoadGameTextures();

            thePlayer = new Objects.Player();

            theObjectManager = new Systems.ObjectManager();
        }
        public void LoadGameTextures()
        {
            theTextureManager.AddTexture("TANK", "Images/GameTank");
            theTextureManager.AddTexture("BULLET", "Images/Bullet");
            theTextureManager.AddFont("MAINFONT", "Fonts/MainFont");
        }
        public override void Exit()
        {
            theObjectManager.Shutdown();
            theTextureManager.ShutdownTextureManager();
            
        }

        public void SpawnTanks(float ElapsedTime)
        {
            TankSpawnCounter += ElapsedTime;
            if (TankSpawnCounter >= 5)
            {
                Objects.EnemyTank newTank = new Objects.EnemyTank();
                theObjectManager.AddObject(newTank);
                TankSpawnCounter = 0;
            }
        }
        public override void GetInput(float ElapsedTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                TopDownShooter.Game1.ChangeState(States.MainMenu.Instance);
        }

        public override void Update(float ElapsedTime)
        {
            GetInput(ElapsedTime);
            thePlayer.Update(ElapsedTime);
            SpawnTanks(ElapsedTime);
            theObjectManager.Update(ElapsedTime);
            theObjectManager.CheckCollisions();
        }
        public override void Render(float ElapsedTime)
        {
            theDevice.Clear(Color.Black);
            theSpriteBatch.Begin();
            thePlayer.Render(ElapsedTime);
            theObjectManager.Render(ElapsedTime);
            theSpriteBatch.DrawString(theTextureManager.GetFont("MAINFONT"), "Score: " + thePlayer.PlayerScore, new Vector2(10, 10), Color.White);
            theSpriteBatch.End();
        }
    }
}